package com.ruoyi.consumer.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.consumer.domain.CUser;
import com.ruoyi.consumer.service.ICUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.consumer.domain.CWalletPerson;
import com.ruoyi.consumer.service.ICWalletPersonService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 个人钱包Controller
 * 
 * @author ruoyi
 * @date 2023-10-29
 */
@RestController
@RequestMapping("/consumer/walletPerson")
public class CWalletPersonController extends BaseController
{
    @Autowired
    private ICWalletPersonService cWalletPersonService;

    @Autowired
    private ICUserService userService;

    /**
     * 查询个人钱包列表
     */
    @PreAuthorize("@ss.hasPermi('consumer:walletPerson:list')")
    @GetMapping("/list")
    public TableDataInfo list(CWalletPerson cWalletPerson)
    {
        startPage();
        List<CWalletPerson> list = cWalletPersonService.selectCWalletPersonList(cWalletPerson);
        list.forEach(item -> {
            CUser cUser = userService.selectCUserByUserId(item.getUserId());
            if (cUser != null) {
                item.setCreateBy(cUser.getUserName());
            } else {
                item.setCreateBy("");
            }
        });

        return getDataTable(list);
    }

    /**
     * 导出个人钱包列表
     */
    @PreAuthorize("@ss.hasPermi('consumer:walletPerson:export')")
    @Log(title = "个人钱包", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CWalletPerson cWalletPerson)
    {
        List<CWalletPerson> list = cWalletPersonService.selectCWalletPersonList(cWalletPerson);
        ExcelUtil<CWalletPerson> util = new ExcelUtil<CWalletPerson>(CWalletPerson.class);
        util.exportExcel(response, list, "个人钱包数据");
    }

    /**
     * 获取个人钱包详细信息
     */
    @PreAuthorize("@ss.hasPermi('consumer:walletPerson:query')")
    @GetMapping(value = "/{userId}")
    public AjaxResult getInfo(@PathVariable("userId") Integer userId)
    {
        return success(cWalletPersonService.selectCWalletPersonByUserId(userId));
    }

    /**
     * 新增个人钱包
     */
    @PreAuthorize("@ss.hasPermi('consumer:walletPerson:add')")
    @Log(title = "个人钱包", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CWalletPerson cWalletPerson)
    {
        return toAjax(cWalletPersonService.insertCWalletPerson(cWalletPerson));
    }

    /**
     * 修改个人钱包
     */
    @PreAuthorize("@ss.hasPermi('consumer:walletPerson:edit')")
    @Log(title = "个人钱包", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CWalletPerson cWalletPerson)
    {
        return toAjax(cWalletPersonService.updateCWalletPerson(cWalletPerson));
    }

    /**
     * 删除个人钱包
     */
    @PreAuthorize("@ss.hasPermi('consumer:walletPerson:remove')")
    @Log(title = "个人钱包", businessType = BusinessType.DELETE)
	@DeleteMapping("/{userIds}")
    public AjaxResult remove(@PathVariable Integer[] userIds)
    {
        return toAjax(cWalletPersonService.deleteCWalletPersonByUserIds(userIds));
    }
}
